package com.drinks.yiniang.ui.fragment.category;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.category.CategoryPageAdapter;
import com.drinks.yiniang.adapter.listview.CategoryPageBannerAdapter;
import com.drinks.yiniang.common.recycler.decoration.CategoryItemDecoration;
import com.drinks.yiniang.common.listener.OnHomeNavigationBarClickListener;
import com.drinks.yiniang.entity.category.CategoryJson;
import com.drinks.yiniang.entity.comment.BannerEntity;
import com.drinks.yiniang.io.api.GoodsApiIO;
import com.drinks.yiniang.ui.activity.category.CategoryActivity;
import com.drinks.yiniang.ui.widget.nav.NavigationHomeBarLayout;
import com.drinks.yiniang.ui.widget.recycler.BaseRecyclerView;
import com.hansen.library.ui.widget.refresh.MSwipeRefreshLayout;
import com.drinks.yiniang.utils.ActivityUtils;
import com.drinks.yiniang.utils.RecyclerUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.fragment.BaseFragment;
import com.hansen.library.ui.widget.listview.NoScrollGridView;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类页面
 *
 * @author han
 * A simple {@link Fragment} subclass.
 */
public class CategoryPageFragment extends BaseFragment implements OnHomeNavigationBarClickListener, SwipeRefreshLayout.OnRefreshListener,
        BaseQuickAdapter.OnItemClickListener , AdapterView.OnItemClickListener {

    private NavigationHomeBarLayout nav_bar_category_page;
    private MSwipeRefreshLayout refresh_category_page;
    //列表
    private BaseRecyclerView rcv_category_page;

    private CategoryPageAdapter mCategoryAdapter;
    private CategoryPageBannerAdapter mBannerAdapter;

    private NoScrollGridView gv_category_page_banner;

    private OnHomeNavigationBarClickListener onHomeNavgationBarClickListener;

    public CategoryPageFragment() {
        // Required empty public constructor
    }

    public static CategoryPageFragment newInstance() {
        return new CategoryPageFragment();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            onHomeNavgationBarClickListener = (OnHomeNavigationBarClickListener) context;
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + " must implement OnHomeNavgationBarClickListener");
        }
    }

    @Override
    protected int getResID() {
        return R.layout.fragment_category_page;
    }

    @Override
    protected void initView(View mView) {
        nav_bar_category_page = mView.findViewById(R.id.nav_bar_category_page);

        refresh_category_page = mView.findViewById(R.id.refresh_category_page);

        rcv_category_page = mView.findViewById(R.id.rcv_category_page);
    }

    @Override
    protected void initData() {
        mContext = getActivity();

        initAdapter();

        getCategoryList();
    }

    private void initAdapter() {
        rcv_category_page.setLayoutManager(RecyclerUtils.getVerticalGridLayout(mContext, 3));
        rcv_category_page.addItemDecoration(CategoryItemDecoration.getItemDPDecoration(20));

        mCategoryAdapter = new CategoryPageAdapter(mContext, new ArrayList<CategoryJson.CategoryList>());
        mCategoryAdapter.setHeaderFooterEmpty(false, true);
        mCategoryAdapter.addFooterView(getFooterView());
        mCategoryAdapter.setCenterEmpty(getLayoutInflater(), rcv_category_page);
        mCategoryAdapter.bindToRecyclerView(rcv_category_page);
    }

    //获取header
    private View getFooterView() {
        View mFooterView = getLayoutInflater().inflate(R.layout.footer_category_page_banner, (ViewGroup) rcv_category_page.getParent(), false);

        gv_category_page_banner = mFooterView.findViewById(R.id.gv_category_page_banner);


        return mFooterView;
    }

    @Override
    protected void listener() {
        nav_bar_category_page.setOnNavgationBarClickListener(this);
        refresh_category_page.setOnRefreshListener(this);
        mCategoryAdapter.setOnItemClickListener(this);
        gv_category_page_banner.setOnItemClickListener(this);
    }

    @Override
    protected void widgetClick(View v) {

    }

    @Override
    public void onAddress(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onAddress(v);
        }
    }

    @Override
    public void onSearchClick(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onSearchClick(v);
        }
    }

    @Override
    public void onMessage(View v) {
        if(onHomeNavgationBarClickListener != null) {
            onHomeNavgationBarClickListener.onMessage(v);
        }
    }

    @Override
    public void onRefresh() {
        refresh_category_page.setRefreshing(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        CategoryJson.CategoryList data = mCategoryAdapter.getItem(position);

        if (data == null) {
            return;
        }

        Intent intent = new Intent(mContext, CategoryActivity.class);
        intent.putExtra(Constants.KeyCategoryId, data.getId());
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(CommonUtils.isArrayIndexOutOfBounds(mBannerAdapter.getList(), position)) {
            return;
        }

        BannerEntity data = mBannerAdapter.getList().get(position);

        ActivityUtils.startActivityByAdType(mContext, data.getType(), data.getValue());
    }

    //获取分类列表
    private void getCategoryList() {
        if (!refresh_category_page.isRefreshing()) {
            showLoadingDialog();
        }

        GoodsApiIO.getInstance().getGoodsCategory(new APIRequestCallback<CategoryJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(CategoryJson res) {
                if (mCategoryAdapter == null) {
                    return;
                }

                mCategoryAdapter.getData().clear();
                mCategoryAdapter.addData(res.getData().getCategories());
                addBanner(res.getData().getBanner());
                mCategoryAdapter.loadMoreComplete();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
                mCategoryAdapter.loadMoreFail();
            }

            @Override
            public void onComplete() {
                refresh_category_page.setRefreshing(false);
                dismissDialog();
            }
        });
    }

    private void addBanner(List<BannerEntity> banner) {
        if(CommonUtils.isEmptyList(banner)) {
            return;
        }

        if(mBannerAdapter == null) {
            mBannerAdapter = new CategoryPageBannerAdapter(mContext, new ArrayList<BannerEntity>());

            gv_category_page_banner.setAdapter(mBannerAdapter);
        }

        mBannerAdapter.addBannerList(banner);
    }


}
